package com.chukun.multi.promise;

import java.io.File;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * 数据同步模块入口类
 */
public class DataSyncTask implements Runnable {


    private final Map<String, String> taskParameters;

    public DataSyncTask(Map<String, String> taskParameters) {
        this.taskParameters = taskParameters;
    }

    @Override
    public void run() {
        String ftpServer = taskParameters.get("ftpServer");
        String userName = taskParameters.get("ftpServer");
        String pwd = taskParameters.get("ftpServer");

        // 先开始初始化ftp客户端实例，此方法越早调用越好
        Future<FTPClientUtil> ftpClientUtilFuture = FTPClientUtil.newInstance(ftpServer, userName, pwd);

        //查询数据库，生成本地文件
        generateFilesFromDB();

        FTPClientUtil ftpClientUtil = null;

        try {
            // 轮询获取结果
            while (!ftpClientUtilFuture.isDone()) {
                ftpClientUtil = ftpClientUtilFuture.get();
            }
        } catch (InterruptedException e) {
            ;
        } catch (ExecutionException e) {
            throw new RuntimeException(e);
        }


    }

    private void generateFilesFromDB() {
        // no handler
    }

    private void uploadFiles(FTPClientUtil ftpClientUtil) {
        Set<File> files = retrieveGeneratedFiles();
        for (File file : files) {
            try {
                ftpClientUtil.upload(file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    private Set<File> retrieveGeneratedFiles() {
        Set<File> files = new HashSet<>();
        return files;
    }
}
